/**

 @Name：CLTPHP 数据库
 @Author：chichu
 @Site：http://www.cltphp.com/admin/
 @License：LPPL
 */

layui.define(['admin', 'table', 'util','form'], function (exports) {
    var $ = layui.$, admin = layui.admin, table = layui.table, element = layui.element,form = layui.form;
    /*****************************数据备份*************************************/
    //全部立标
    tableIn = table.render({
        elem: '#list',
        url: '/shengteng/database/database',
        method: 'post',
        cols: [[
            {type: "checkbox", fixed: true},
            {field: 'name', title: '数据库表', width: 150, fixed: true,sort: true},
            {field: 'rows', title: '记录条数', width: 150,sort: true},
            {field: 'size', title: '占用空间', width: 150,sort: true},
            {field: 'engine', title: '类型', width: 110,sort: true},
            {field: 'collation', title: '编码', width: 150,sort: true},
            {field: 'create_time', title: '创建时间', width: 180,sort: true},
            {field: 'comment', title: '说明', width: 180}
        ]],
        done: function(res, curr, count){
            $('.count').html(res.tableNum);
            $('.size').html(res.total);
        }
    });
    var events = {
        back: function(){
            var obj = $(this);
            var checkStatus = table.checkStatus('list'),data = checkStatus.data; //获得选中的数据
            if(data.length === 0) return layer.msg('未选中数据表');
            var tables = [];
            $.each(data, function(i,o){
                tables.push(o.name);
            });
            obj.addClass('layui-btn-disabled');
            obj.html('备份进行中...');
            admin.req({
                url: '/shengteng/database/backup/'
                , type: 'post'
                , data: {tables:tables}
                , success: function (res) {
                    if(res.code>0){
                        layer.msg(res.msg, {icon: 2, time: 1000});
                    }else{
                        layer.msg(res.msg, {icon: 1, time: 1000});
                    }
                    obj.removeClass('layui-btn-disabled');
                    obj.html('备份');
                }
            });
        },
        optimize: function(){
            var obj = $(this);
            var checkStatus = table.checkStatus('list'),data = checkStatus.data; //获得选中的数据
            if(data.length === 0) return layer.msg('未选中数据表');
            var tables = [];
            $.each(data, function(i,o){
                tables.push(o.name);
            });
            obj.addClass('layui-btn-disabled');
            obj.html('优化进行中...');
            admin.req({
                url: '/shengteng/database/optimize/'
                , type: 'post'
                , data: {tables:tables}
                , success: function (res) {
                    if(res.code>0){
                        layer.msg(res.msg, {icon: 2, time: 1000});
                    }else{
                        layer.msg(res.msg, {icon: 1, time: 1000});
                    }
                    obj.removeClass('layui-btn-disabled');
                    obj.html('优化');
                    tableIn.reload();
                }
            });
        },
        repair: function(){
            var obj = $(this);
            var checkStatus = table.checkStatus('list'),data = checkStatus.data; //获得选中的数据
            if(data.length === 0) return layer.msg('未选中数据表');
            var tables = [];
            $.each(data, function(i,o){
                tables.push(o.name);
            });
            obj.addClass('layui-btn-disabled');
            obj.html('修复进行中...');
            admin.req({
                url: '/shengteng/database/repair/'
                , type: 'post'
                , data: {tables:tables}
                , success: function (res) {
                    if(res.code>0){
                        layer.msg(res.msg, {icon: 2, time: 1000});
                    }else{
                        layer.msg(res.msg, {icon: 1, time: 1000});
                    }
                    obj.removeClass('layui-btn-disabled');
                    obj.html('修复');
                    tableIn.reload();
                }
            });
        }
    };
    $('.clt-auth-btns .layui-btn').on('click', function(){
        var othis = $(this),thisEvent = othis.data('events');
        events[thisEvent] && events[thisEvent].call(this);
    });
    /*****************************备份文件*************************************/
    tableIn = table.render({
        elem: '#restore',
        url: '/shengteng/database/restore',
        method: 'post',
        cols: [[
            {field:'name', title: '文件名称', width:250}
            ,{field:'size', title: '文件大小', width:200,sort:true,templet:'#size'}
            ,{field:'addtime', title: '备份时间', width:200,sort:true}
            ,{field:'part', title: '卷号', width:150}
            ,{width:160, align:'center', toolbar: '#action'}
        ]]
    });
    //监听工具条
    table.on('tool(restore)', function(obj){
        var data = obj.data;
        var time = data.time;
        if(obj.event === 'del'){
            layer.confirm('您确定要删除该备份文件吗？', function(index){
                admin.req({
                    url: '/shengteng/database/delSqlFiles/'
                    , type: 'post'
                    , data: {'time':time}
                    , success: function (res) {
                        layer.closeAll('dialog');
                        if(res.code==0){
                            layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                                obj.del();
                            });
                        }
                    }
                });
            });
        } else if(obj.event === 'recover'){
            layer.confirm('您确定要还原该备份文件吗？', function(index){
                admin.req({
                    url: '/shengteng/database/import/'
                    , type: 'post'
                    , data: {'time':time}
                    , success: function (res) {
                        layer.closeAll('dialog');
                        if(res.code==0){
                            layer.msg(res.msg, {icon: 1, time: 1000});
                        }
                    }
                });
            });
        }
    });
    exports('database', {});
});
